Conditions | 10 |
Paths | 48 |
Total Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Server.request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
67 | Server.prototype.request = function (options, btn) { |
||
68 | var ajaxOptions = { |
||
69 | url: '', |
||
70 | type: 'GET', |
||
71 | dataType: 'json', |
||
72 | data: {}, |
||
73 | async: true, |
||
74 | contentType: false, |
||
75 | cache: false, |
||
76 | }; |
||
77 | for (var key in options) { |
||
78 | ajaxOptions[key] = options[key]; |
||
79 | } |
||
80 | if (options.url && options.url.indexOf('http:') !== 0 && options.url.indexOf('https:') !== 0 && options.url.indexOf(inji.options.appRoot) !== 0) { |
||
81 | ajaxOptions.url = inji.options.appRoot + (options.url.replace(/^\//g, '')); |
||
82 | } |
||
83 | if (typeof btn != 'undefined') { |
||
84 | if (!$(btn).data('loading-text')) { |
||
85 | $(btn).data('loading-text', 'подождите'); |
||
86 | } |
||
87 | var btn = $(btn).button().button('loading'); |
||
88 | } |
||
89 | var callback = null; |
||
90 | if (typeof options.success != 'undefined') { |
||
91 | callback = options.success; |
||
92 | } |
||
93 | ajaxOptions.success = function (data, textStatus, jqXHR) { |
||
94 | if (typeof btn != 'undefined') { |
||
95 | btn.button('reset'); |
||
96 | } |
||
97 | if (ajaxOptions.dataType != 'json') { |
||
98 | callback(data, textStatus, jqXHR); |
||
99 | } else { |
||
100 | if (data.success) { |
||
101 | if (data.successMsg) { |
||
102 | noty({text: data.successMsg, type: 'success', timeout: 3500, layout: 'center'}); |
||
103 | } |
||
104 | if (typeof data.scripts == 'object') { |
||
105 | inji.loaded = false; |
||
106 | inji.onLoad(function () { |
||
107 | inji.Server.runCommands(data.commands); |
||
108 | if (callback !== null) { |
||
109 | callback(data.content, textStatus, jqXHR) |
||
110 | } |
||
111 | }); |
||
112 | if (data.scripts.length > 0) { |
||
113 | inji.loadScripts(data.scripts, 0); |
||
114 | } else { |
||
115 | inji.startCallbacks(); |
||
116 | } |
||
117 | } else { |
||
118 | inji.Server.runCommands(data.commands); |
||
119 | if (callback !== null) { |
||
120 | callback(data.content, textStatus, jqXHR); |
||
121 | } |
||
122 | } |
||
123 | } else { |
||
124 | inji.Server.runCommands(data.commands); |
||
125 | noty({text: data.error, type: 'warning', timeout: 3500, layout: 'center'}); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | var errorCallback = null; |
||
130 | if (typeof options.error != 'undefined') { |
||
131 | errorCallback = options.error; |
||
132 | } |
||
133 | ajaxOptions.error = function (jqXHR, textStatus, errorThrown) { |
||
134 | if (typeof btn != 'undefined') { |
||
135 | btn.button('reset'); |
||
136 | } |
||
137 | if (errorCallback != null) { |
||
138 | errorCallback(jqXHR, textStatus, errorThrown); |
||
139 | } else if (textStatus != 'abort') { |
||
140 | noty({ |
||
141 | text: 'Во время запроса произошла ошибка: ' + textStatus, |
||
142 | type: 'warning', |
||
143 | timeout: 3500, |
||
144 | layout: 'center' |
||
145 | }); |
||
146 | } |
||
147 | } |
||
148 | return $.ajax(ajaxOptions); |
||
149 | }; |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: